package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.FeedDao;
import cn.sumpu.app.guoguojie.domain.Feed;
import cn.sumpu.app.guoguojie.domain.Ext.FeedExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class FeedDaoImpl extends SqlSessionDaoSupport implements FeedDao {
	private static Logger logger = LoggerFactory.getLogger(FeedDaoImpl.class);

	public void insert(Feed feed) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.FeedMapper.insert",
					feed);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}

	}

	@SuppressWarnings("unchecked")
	public List<FeedExt> getFeedByGroup(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedByGroup",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void updateFeed(Feed feed) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.FeedMapper.updateFeed",
					feed);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update contact failure.", e);
			}
			throw new AppBusinessException("update contact failure.", e);
		}

	}

	public void deleteFeedById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.FeedMapper.deleteFeedById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete failure.", e);
			}
			throw new AppBusinessException("delete failure.", e);
		}

	}

	@Override
	public Feed getFeedById(String id) throws AppBusinessException {
		try {
			return (Feed) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query failure.", e);
			}
			throw new AppBusinessException("query failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<FeedExt> getFriendFamilyAllFeedList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFriendFamilyAllFeedList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<FeedExt> getMyFamilyFeedList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getMyFamilyFeedList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void deleteBlogFeed(String blogId) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.FeedMapper.deleteBlogFeed",
							blogId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete failure.", e);
			}
			throw new AppBusinessException("delete failure.", e);
		}

	}

	@Override
	public int getFriendFamilyAllFeedListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFriendFamilyAllFeedListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFriendFamilyAllFeedListCount failure.", e);
			}
			throw new AppBusinessException(
					"getFriendFamilyAllFeedListCount failure.", e);
		}
	}

	@Override
	public int getFeedByGroupCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedByGroupCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedByGroupCount failure.", e);
			}
			throw new AppBusinessException("getFeedByGroupCount failure.", e);
		}
	}

	@Override
	public void deleteFeed(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession().delete(
					"cn.sumpu.app.guoguojie.persistence.FeedMapper.deleteFeed",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedByGroupCount deleteFeed.", e);
			}
			throw new AppBusinessException("getFeedByGroupCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<FeedExt> getDoingFeed(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getDoingFeed",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public int getFeedCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedCount failure.", e);
			}
			throw new AppBusinessException("getFeedCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<FeedExt> getDoingByClassify(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getDoingByClassify",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	@SuppressWarnings("unchecked")
	public List<FeedExt> getAllFeedByClassify(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getAllFeedByClassify",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<FeedExt> getFeedByGroupClassify(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedByGroupClassify",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	public int getDoingFeedCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getDoingFeedCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedCount failure.", e);
			}
			throw new AppBusinessException("getDoingFeedCount failure.", e);
		}
	}

	public int getDoingByClassifyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getDoingByClassifyCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedCount failure.", e);
			}
			throw new AppBusinessException("getDoingByClassifyCount failure.", e);
		}
	}

	@Override
	public int getFeedByGroupClassifyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedByGroupClassifyCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedCount failure.", e);
			}
			throw new AppBusinessException("getFeedByGroupClassifyCount failure.", e);
		}
	}

	@Override
	public int getFriendFamilyAllFeedCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFriendFamilyAllFeedCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedCount failure.", e);
			}
			throw new AppBusinessException("getFriendFamilyAllFeedCount failure.", e);
		}
	}

	@Override
	public int getAllFeedByClassifyCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getAllFeedByClassifyCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedCount failure.", e);
			}
			throw new AppBusinessException("getAllFeedByClassifyCount failure.", e);
		}
	}
	
	/**
	 * author Bruce Zhang
	 * @param map	门牌号和动态类型
	 * @return 他家的动态
	 * @throws AppBusinessException
	 */
	@SuppressWarnings("unchecked")
	public List<FeedExt> getFeedByDoorplate(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedByDoorplate",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedByDoorplate database failure.", e);
			}
			throw new AppBusinessException("getFeedByDoorplate database failure.", e);
		}
	}
	
	/**
	 * author Bruce 	Zhang
	 * @param String	门牌号
	 * @return 他家的动态的总记录数
	 * @throws AppBusinessException
	 */
	@Override
	public Integer getFeedByDoorplateCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FeedMapper.getFeedByDoorplateCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFeedByDoorplateCount failure.", e);
			}
			throw new AppBusinessException("getFeedByDoorplateCount failure.", e);
		}
	}

}
